package com.zhuiyun.educations.api.live.controller;

import com.zhuiyun.educations.api.live.entity.LiveEntity;
import com.zhuiyun.educations.api.live.service.LiveService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.TencentYunVedioUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import java.util.HashMap;

import static com.zhuiyun.educations.util.TencentYunVedioUtil.*;

/**
 * @ClassName LiveController
 * @Description TODO
 * @Author cjl
 * @Date 2023/6/28 10:47
 **/
@RestController
@RequestMapping("/live")
public class LiveController {

    @Autowired
    LiveService liveService;
    /**
    * @Author cjl
    * @Description TODO 关闭直播
    * @Date 2023/6/30 14:16
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param id
    */
    @GetMapping("/closeLive")
    public CommonResult closeLive(Integer id) {
        return liveService.closeLive(id);
    }
    /**
    * @Author cjl
    * @Description TODO 获取正在直播的直播间
    * @Date 2023/6/30 14:16
    * @Return com.zhuiyun.educations.common.response.CommonResult
    */
    @GetMapping("listLive")
    public CommonResult listLive(){
        return liveService.listLive();
    }

    /**
    * @Author cjl
    * @Description TODO 检验当前主播是否正在直播
    * @Date 2023/6/30 14:16
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param userId
    */
    @GetMapping("checkLiveByUserId")
    public CommonResult checkLiveByUserId(Integer userId){
        return liveService.checkLiveByUserId(userId);
    }
    /**
    * @Author cjl
    * @Description TODO 根据id获取直播间 信息
    * @Date 2023/6/30 14:16
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param id
    */
    @GetMapping("getLiveById")
    public CommonResult getLiveById(Integer id){
        return liveService.getLiveById(id);
    }

    /**
    * @Author cjl
    * @Description TODO 添加直播间
    * @Date 2023/6/29 8:34
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param liveEntity
    * @param multipartRequest
    */
    @PostMapping("/saveLive")
    public CommonResult saveList(LiveEntity liveEntity, MultipartRequest multipartRequest){
        return liveService.saveLive(liveEntity,multipartRequest);
    }

    /**
    * @Author cjl
    * @Description TODO 根据用户id获取直播信息
    * @Date 2023/6/28 19:44
    * @Return com.zhuiyun.educations.common.response.CommonResult
    * @param userId
    */
    @GetMapping("/getLiveByUserId")
    CommonResult getLiveByUserId(Integer userId){
        return liveService.getLiveByUserId(userId);
    }
    /**
     *  TODO    * 生成推流全路径地址
     *      * @param pushUrl 推流地址 头部
     *      * @param stream_id 直播码  调用该方法genLiveCode() 生成
     *      * @param push_key 推流鉴权
     *      * @return
     *
     */
    @PostMapping("createPushUrlAndPlayUrl")
    public static HashMap<Object, Object> createPushUrlAndPlayUrl(String streamName) {
        //过期时间 16进制的unix时间戳
        String txTime = TencentYunVedioUtil.to16Hex(addOneDay());
        // 生成推流地址
        StringBuffer sb = new StringBuffer();
        String pushUrlHead= TencentYunVedioUtil.pushUrl + streamName;
        // 生成串流密钥
        sb.append(streamName).append("?").append("txSecret=")
                .append(genSign(push_key, streamName,txTime)).append("&").append("txTime=")
                .append(txTime);
        // 生成播流地址
        StringBuffer sb1 = new StringBuffer();
        sb1.append(getPlayUrlHlsM(streamName)).append("?").append("txSecret=").append(pushSign(play_key, streamName, txTime))
                .append("&")
                .append("txTime=")
                .append(txTime);
        HashMap<Object, Object> map = new HashMap<>();
        // 推流地址
        map.put("streamKey", sb);
        // 播流地址
        map.put("playUrl", sb1);
        // 服务器地址
        map.put("pushUrlHead", TencentYunVedioUtil.pushUrl);
        return map;
    }
    /**
     *   TODO    * 生成拉流全路径地址
     *      * @param playUrlHead  拉流的头地址
     *      * @param stream_id  直播码  调用该方法genLiveCode() 生成
     *      * @param play_key  拉流鉴权
     *      * @param end 如 .flv结尾
     *      * @return
     */
    @PostMapping("createPlayUrl")
    public static String createPlayUrl(String streamName) {
        //过期时间 16进制的unix时间戳
        String txTime = to16Hex(addOneDay());
        StringBuffer sb = new StringBuffer();
        //String stream_id = TencentYunVedioUtil.bizid();
        sb.append(getPlayUrlHlsM(streamName)).append("?").append("txSecret=").append(pushSign(play_key, streamName, txTime))
                .append("&")
                .append("txTime=")
                .append(txTime);
        return sb.toString();
    }

}
